﻿using B.T.BasicData.ErrorCode;
using B.T.BasicData.Write_Api.DTO;
using B.T.BasicData.Write_Api.RBAC.IServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.T.BasicData.Write_Api.Controllers
{
    /// <summary>
    /// 角色管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly ILogger<RoleController> logger;
        private readonly IRoleServices roleServices;

        public RoleController(ILogger<RoleController> logger,IRoleServices roleServices)
        {
            this.logger = logger;
            this.roleServices = roleServices;
        }


        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        [Authorize]
        public async Task<ApiResult> AddRole(AddRoleDto dto)
        {
            try
            {
                return await roleServices.AddRole(dto);
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpDelete]
        [Authorize]
        public async Task<ApiResult> DeleRole(DeleRoleDto dto)
        {
            try
            {
                return await roleServices.DeleRole(dto);
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// 修改角色信息及权限
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        public async Task<ApiResult> UpdateRole(UpdateRoleDto dto)
        {
            try
            {
                return await roleServices.UpdateRole(dto);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }
    }
}
